package autoteam.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Date;
import java.util.EventObject;

import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.TransferHandler;
import javax.swing.table.TableColumn;

import mseries.ui.MDateCellEditor;
import autoteam.tables.AutoTeamTableModel;
import autoteam.tables.TableSorter;

// import javax.swing.plaf.UIResource;

public class AutoTeamTablePanel extends JPanel
{
    private static final long serialVersionUID = 5073197267250740852L;

    JTable table;
    TableSorter sorter;
    AutoTeamTableModel model;
    JPanel buttonPanel;

    public AutoTeamTablePanel( AutoTeamTableModel _model )
    {
    	this( _model, new JButton( "?"));
    }
    
    public AutoTeamTablePanel( AutoTeamTableModel _model, JButton ... buttons )
    {
	setLayout( new BorderLayout( ) );
	
	model = _model;
	// create table
	
	sorter = new TableSorter( model );
	table = new JTable( sorter );
	// sorter.addMouseListenerToHeaderInTable(table);
	sorter.setTableHeader( table.getTableHeader( ) );
	
	// set table properties
	table.setMaximumSize( new Dimension( 1600, 720 ) );
	table.setPreferredScrollableViewportSize( new Dimension( 720, 400 ) );
	table.setMinimumSize( new Dimension( 480, 320 ) );
	table.setRowSelectionAllowed( true );
	
	// table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	
	JScrollPane tableScrollPane = new JScrollPane( table );
	
	// For layout purposes, put the buttons in a separate panel
	buttonPanel = new JPanel( );
	for( int i = 0; i < (buttons.length); i++ )
	{
		if (buttons[i].getText().equals("?"))
			;
		else
			addButton( buttons[i] );
	}
	add( buttonPanel, BorderLayout.NORTH );
	add( tableScrollPane, BorderLayout.CENTER );
    }
    
    public JTable getTable()
    {
	return table;
    }
    
    public void addButton( JButton button )
    {
	buttonPanel.add( button );
    }
    
    public void addComponent( JComponent component )
    {
	buttonPanel.add( component );
    }
    
    public void enableDrag( )
    {
	table.setTransferHandler( new ObjectTransferHandler( ) );
	table.setDragEnabled( true );
	System.out.println( "Transfer: " + table.getTransferHandler( ) );
    }
    
    public void stopEditing( )
    {
	if ( table.isEditing( ) )
	{
	    table.getCellEditor( ).stopCellEditing( );
	}
    }
    
    public int getSelectedRow( )
    {
	System.out.println( "Selected row = " + sorter.modelIndex( table.getSelectedRow( ) ) );
	
	return sorter.modelIndex( table.getSelectedRow( ) );
    }
    
    public void setRowSelectionInterval( int r, int s )
    {
	table.setRowSelectionInterval( r, s );
    }
    

    
    public void addPopupMenu( final JPopupMenu popup )
    {
	// Add listener to components that can bring up popup menus.
	
	table.addMouseListener( new MouseAdapter( )
	{
	    public void mousePressed( MouseEvent e )
	    {
		maybeShowPopup( e );
	    }
	    
	    public void mouseReleased( MouseEvent e )
	    {
		maybeShowPopup( e );
	    }
	    
	    private void maybeShowPopup( MouseEvent e )
	    {
		if ( e.isPopupTrigger( ) )
		{
		    if ( getSelectedRow( ) >= 0 )
			popup.show( e.getComponent( ), e.getX( ), e.getY( ) );
		}
	    }
	} );
    }
    
    public void setTableCellEditor( int column, JComboBox editor )
    {
	TableColumn theColumn = table.getColumnModel( ).getColumn( column );
	DefaultCellEditor cellEditor = new DefaultCellEditor( editor );
	cellEditor.setClickCountToStart( 2 );
	theColumn.setCellEditor( cellEditor );
    }
    
    public void setTableCellEditor( Class c, JComboBox editor )
    {
	DefaultCellEditor cellEditor = new DefaultCellEditor( editor );
	cellEditor.setClickCountToStart( 2 );
	table.setDefaultEditor( c, cellEditor );
    }
    
    public void setTableDateCellEditor( )
    {
	// cellEditor.setClickCountToStart(2);
	table.setDefaultEditor( Date.class, new MDateCellEditor( "dd MMMMMyyyy" )
	{
	    public boolean isCellEditable( EventObject anEvent )
	    {
		if ( anEvent instanceof MouseEvent )
		{
		    return ((MouseEvent) anEvent).getClickCount( ) >= 2;
		}
		return true;
	    }
	} );
    }
    
    public Object getSelectedObject( )
    {
	int selRow = getSelectedRow( );
	return model.getSelectedObject( selRow );
	// return sorter.modelIndex(getSelectedObject(selRow));
    }
    
    public static DataFlavor objectFlavor =
	new DataFlavor( DataFlavor.javaJVMLocalObjectMimeType + ";class=java.lang.Object", "An arbitrary Object" );
    private static final DataFlavor[] flavors = { objectFlavor };
    
    // DND Helpers
    class ObjectTransferable implements Transferable
    {
	// private final DataFlavor[] flavors = {objectFlavor};
	private Object data = null;
	
	public ObjectTransferable( Object data )
	{
	    this.data = (Object) data;
	}
	
	public DataFlavor[] getTransferDataFlavors( )
	{
	    return (DataFlavor[]) flavors.clone( );
	}
	
	public boolean isDataFlavorSupported( DataFlavor flavor )
	{
	    return flavor.equals( objectFlavor );
	}
	
	public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException
	{
	    if ( flavor.equals( objectFlavor ) )
	    {
		return (Object) data;
	    }
	    else
	    {
		throw new UnsupportedFlavorException( flavor );
	    }
	}
    }
    
    class ObjectTransferHandler extends TransferHandler
    {
	public ObjectTransferHandler( )
	{
	    super( );
	}
	
	public boolean canImport( JComponent comp, DataFlavor[] transferFlavors )
	{
	    for ( int i = 0; i < transferFlavors.length; i++ )
		if ( !transferFlavors[i].equals( objectFlavor ) )
		    return false;
	    return true;
	}
	
	protected Transferable createTransferable( JComponent c )
	{
	    Object g = getSelectedObject( );
	    return new ObjectTransferable( g );
	}
	
	public boolean importData( JComponent comp, Transferable t )
	{
	    try
	    {
		model.handleDrop( t.getTransferData( objectFlavor ), getSelectedRow( ) );
	    }
	    catch ( UnsupportedFlavorException e )
	    {
		e.printStackTrace( );
	    }
	    catch ( IOException e )
	    {
		e.printStackTrace( );
	    }
	    return true;
	}
	
	public int getSourceActions( JComponent c )
	{
	    return COPY;
	}
	
    }
}
